package com.example.pricecloudperformancetesting.create;

import com.example.pricecloudperformancetesting.entity.QuoteHeartbeat;
import com.example.pricecloudperformancetesting.entity.QuoteSymbol;
import com.example.pricecloudperformancetesting.entity.SymbolConfig;
import com.example.pricecloudperformancetesting.mapper.SymbolConfigDAO;
import com.example.pricecloudperformancetesting.utils.DealWithSymbolQuoteDepth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.testng.annotations.Test;

import javax.annotation.PostConstruct;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Remy
 * @description :
 * @date 2020-05-21 17:50
 */
@Configuration
@EnableScheduling
public class CreateSymbolQuote {

    @Autowired
    SymbolConfigDAO symbolConfigImpl;

    private static List<SymbolConfig> symbolConfigList = null;

    @PostConstruct
    public void init() {

        symbolConfigList = symbolConfigImpl.selectAllSymbolConfig();
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    private void configureTasks() {
        symbolConfigList = symbolConfigImpl.selectAllSymbolConfig();
    }

    public List<QuoteSymbol> createQuote() {
        List<QuoteSymbol> quoteSymbolList = new ArrayList<>();
        for (SymbolConfig symbolConfig : symbolConfigList) {
            quoteSymbolList.add(QuoteSymbol.builder().symbol(symbolConfig.getSymbol()).time(System.currentTimeMillis() / 1000)
                    .depth(symbolConfig.getDepth()).ticks(DealWithSymbolQuoteDepth.readConfigDealWithQuoteDepth(symbolConfig))
                    .build());
        }
        return quoteSymbolList;
    }

    public List<QuoteHeartbeat> createQuoteHeartbeat() {
        List<QuoteSymbol> quoteSymbolList = this.createQuote();
        List<QuoteHeartbeat> quoteHeartbeatList = new ArrayList<>();
        for (QuoteSymbol quoteSymbol : quoteSymbolList) {
            QuoteHeartbeat quoteHeartbeat = QuoteHeartbeat.builder().dateType(2).dataTime(System.currentTimeMillis() / 1000).version("1.0.0")
                    .data(quoteSymbol).key(md5("2")).build();
            quoteHeartbeatList.add(quoteHeartbeat);
        }
        return quoteHeartbeatList;
    }

    // key = md5(data_type + data_time + version + secretKey)
    public static String md5(String dateType) {
        String version = "1.0.0";
        String secretKey = "htrh6)(4j5345#jkld";
        Long s = System.currentTimeMillis()/1000;
        String time=s.toString();
        if (dateType == null) {
            throw new RuntimeException("dateType is null");
        }
        String keys = dateType + time + version + secretKey;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(keys.getBytes());
            byte[] encryContext = md.digest();

            int i;
            StringBuffer buf = new StringBuffer("");
            //做相应的转化（十六进制）
            for (int offset = 0; offset < encryContext.length; offset++) {
                i = encryContext[offset];
                if (i < 0) {
                    i += 256;
                }
                if (i < 16) {
                    buf.append("0");
                }
                buf.append(Integer.toHexString(i));
            }
            // 32位的加密
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        return null;
    }

}
